package com.bulletelc.apollopre.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.bulletelc.apollopre.MyApplication;
import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.constants.GlobalConfig;
import com.bulletelc.apollopre.entity.WxShareBean;
import com.bulletelc.apollopre.utils.MyUtil;
import com.tencent.connect.share.QQShare;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

/**
 * 分享弹框
 * Created by DELL on 2016/10/13.
 */

public class ShareDialog implements View.OnClickListener, IWXAPIEventHandler {
    private Context context;
    private Dialog dialog;

    private Tencent mTencent;

    /**
     * 分享的内容
     */
    private String url;
    private String title;
    private String icon;
    private String des;
    private WxShareBean wxShareBean;
    /**
     * 微信分享的图片bitmap
     */
    private Bitmap bitmap;

    /**
     * @param context 上下文
     */
    public ShareDialog(Context context, Tencent mTencent) {
        this.context = context;
        this.mTencent = mTencent;
    }

    public void setWxShareBean(WxShareBean wxShareBean) {
        this.url = wxShareBean.getUrl();
        this.title = wxShareBean.getTitle();
        this.des = wxShareBean.getDes();
        this.icon = wxShareBean.getIcon();
    }

    public void setWxShareBean(String url, String title, String des, String icon) {
        this.url = url;
        this.title = title;
        this.des = des;
        this.icon = icon;
    }

    public void setBitmap(Bitmap bitmap) {
        this.bitmap = bitmap;
    }

    public void showShareDialog() {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_share, null);
        dialog = new Dialog(context, R.style.common_dialog);
        dialog.setContentView(view);
        dialog.show();

        //初始化控件
        ViewGroup llChatShare = (ViewGroup) view.findViewById(R.id.llChatShare);
        ViewGroup llChatQuanShare = view.findViewById(R.id.llChatQuanShare);
        ViewGroup llQQShare = (ViewGroup) view.findViewById(R.id.llQQShare);
        ImageView imageView = (ImageView) view.findViewById(R.id.ivCancelShare);
        //设置监听
        llChatShare.setOnClickListener(this);
        llChatQuanShare.setOnClickListener(this);
        llQQShare.setOnClickListener(this);
        imageView.setOnClickListener(this);

        Window window = dialog.getWindow();
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.gravity = Gravity.BOTTOM;
        window.setAttributes(params);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.llChatShare://微信好友分享
                chatShare(1);
                break;

            case R.id.llChatQuanShare: //微信朋友圈分享
                chatShare(2);
                break;

            case R.id.llQQShare://qq分享
                qqShare();
                break;
            case R.id.ivCancelShare://取消

                break;
        }
        dialog.dismiss();
        //dialog.cancel();
    }

    /**
     * 微信分享
     */
    private void chatShare(int type) {
        //初始化一个WXWebpageObject对象,填写url
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = url;

        //用WXWebpageObject对象初始化一个WXMediaMessage对象,填写标题、描述
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.description = des;

        //设置图片
        if (bitmap != null) {
            msg.thumbData = MyUtil.bmpToByteArray(bitmap, false);//给false
        }

        //构造一个Req
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("webpage");
        req.message = msg;
        if (type == 1) {
            msg.title = title;
            req.scene = SendMessageToWX.Req.WXSceneSession;//分享到会话
        } else if (type == 2) {
            msg.title = des;
            req.scene = SendMessageToWX.Req.WXSceneTimeline;//分享到朋友圈
        }

        MyApplication.getInstance().getIwxapi().sendReq(req);

    }

    // 过滤发送内容格式
    private String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }

    /**
     * 分享到qq
     */
    private void qqShare() {
        Bundle bundle = new Bundle();
        bundle.putString(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT + "");
        //分享的标题
        bundle.putString(QQShare.SHARE_TO_QQ_TITLE, title);
        //这条分享消息被好友点击后的跳转URL。
        bundle.putString(QQShare.SHARE_TO_QQ_TARGET_URL, url);
        //分享的应用名称。注：PARAM_TITLE、PARAM_IMAGE_URL、PARAM_	SUMMARY不能全为空，最少必须有一个是有值的。
        bundle.putString(QQShare.SHARE_TO_QQ_APP_NAME, title + GlobalConfig.QQ_APP_ID);
        //分享的图片URL
        bundle.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, icon);
        //分享的消息摘要，最长50个字
        bundle.putString(QQShare.SHARE_TO_QQ_SUMMARY, des);

        mTencent.shareToQQ((Activity) context, bundle, new BaseUiListener());

    }

    /**
     * 微信发送的请求将回调到onReq方法
     *
     * @param baseReq
     */
    @Override
    public void onReq(BaseReq baseReq) {

    }

    /**
     * 发送到微信请求的响应结果将回调到onResp方法
     *
     * @param baseResp
     */
    @Override
    public void onResp(BaseResp baseResp) {
//        WeChatEntryEventBus event = new WeChatEntryEventBus();
//        event.setBaseResp(baseResp);
//        EventBus.getDefault().post(event);

    }

    private class BaseUiListener implements IUiListener {

        @Override
        public void onComplete(Object o) {
        }

        @Override
        public void onError(UiError uiError) {
        }

        @Override
        public void onCancel() {
        }
    }


}
